package models

import (
	"context"
	"go-chain-api/global"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"log"
)

// UpCoin  充币记录
type UpCoin struct {
	ID           primitive.ObjectID `bson:"_id,omitempty" json:"id"`
	BlockHash    string             `bson:"blockHash" json:"blockHash"`
	TxHash       string             `bson:"txHash" json:"txHash"`
	From         string             `bson:"from" json:"from"`
	To           string             `bson:"to" json:"to"`
	Value        string             `bson:"value" json:"value"`
	Data         string             `bson:"data" json:"data"`
	ChainId      uint               `bson:"chainId" json:"chainId"`
	Timestamp    uint               `bson:"timestamp" json:"timestamp"`
	BlockNumber  uint               `bson:"blockNumber" json:"blockNumber"`
	TokenAddress string             `bson:"tokenAddress" json:"tokenAddress"`
	ChainName    string             `bson:"chainName" json:"chainName"`
	SymbolName   string             `bson:"symbolName" json:"symbolName"`
	Status       int                `bson:"status" json:"status"`
}

func GetUpCoinAddress(chainID uint, chainName string) ([]string, error) {
	var tokenAddressListStruct []*WatchAddressConfig
	addressClient := global.DBEngine.Database(global.DbConfig.DbName).Collection("watch_address_config")
	find, err := addressClient.Find(context.Background(), bson.D{{"chainId", chainID}, {"chainName", chainName}, {"type", "udun"}})
	if err != nil {
		log.Printf("TRX充币监控，获取address列表失败 %s", err.Error())
		return nil, err
	}
	err = find.All(context.Background(), &tokenAddressListStruct)
	if err != nil {
		log.Printf("TRX充币监控，解析数据失败 %s", err.Error())
		return nil, err
	}

	var addressList []string
	for _, token := range tokenAddressListStruct {
		addressList = append(addressList, token.Address)
	}
	return addressList, nil
}
